<!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
    xmlns:s="http://jboss.com/products/seam/taglib"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:rich="http://richfaces.org/rich"
    xmlns:a4j="http://richfaces.org/a4j"
    template="/layout/template.xhtml">

<ui:define name="body">

    <h:form id="recipeForm">
        <rich:panel>
            <f:facet name="header">Recipe</f:facet>

            <s:decorate id="nameField" template="/layout/edit.xhtml">
                <ui:define name="label">Name</ui:define>
                <h:inputText id="name" 
                       required="true"
                          value="#{recipeHome.instance.name}"/>
            </s:decorate>

            <s:decorate id="numberOfPersonsField" template="/layout/edit.xhtml">
                <ui:define name="label">Number Of Persons</ui:define>
                <h:inputText id="numberOfPersons" 
                       required="true"
                          value="#{recipeHome.instance.numberOfPersons}"/>
            </s:decorate>
                
            <s:decorate id="preparationDescriptionField" template="/layout/edit.xhtml">
                <ui:define name="label">Preparation Description</ui:define>
                <h:inputTextarea id="preparationDescription"
                               cols="100"
                               rows="10"
                           required="true"
                              value="#{recipeHome.instance.preparationDescription}" />
            </s:decorate>
            
            <div style="clear:both"/>
            <h:form id="recipeIngredientForm">
            <s:validateAll />
            <rich:dataTable id="ingredientList" var="ingredient" value="#{ingredients}">
                    <f:facet name="header">Ingredients</f:facet>
                    <rich:column>
                        <f:facet name="header">Quantity</f:facet>
                        <h:outputText value="#{ingredient.quantity.quantity}" rendered="#{toEditRecipeIngredient != ingredient}" />
                        <h:inputText value="#{ingredient.quantity.quantity}" rendered="#{toEditRecipeIngredient == ingredient}" />
                    </rich:column>
                    <rich:column>
                        <f:facet name="header">Pieces</f:facet>
                        <h:outputText value="#{ingredient.quantity.numberOfPieces}" rendered="#{toEditRecipeIngredient != ingredient}" />
                        <h:inputText value="#{ingredient.quantity.numberOfPieces}" rendered="#{toEditRecipeIngredient == ingredient}" />
                    </rich:column>
                    <rich:column>
                        <f:facet name="header">Unit</f:facet>
                        <h:outputText value="#{ingredient.quantity.unit.name}" rendered="#{toEditRecipeIngredient != ingredient}" />
                        <h:selectOneMenu  id="unit" value="#{ingredient.quantity.unit}" rendered="#{toEditRecipeIngredient == ingredient}" >
                            <s:selectItems noSelectionLabel="- Select a Unit -" value="#{stockUnitList.resultList}" var="unit" label="#{unit.name}" />
                            <s:convertEntity />
                        </h:selectOneMenu>
                    </rich:column>
                    <rich:column styleClass="#{not empty facesMessages.getCurrentMessagesForControl('product') ? 'errors' : ''}">
                        <f:facet name="header">Name</f:facet>
                        <h:outputText value="#{ingredient.product.name}" rendered="#{toEditRecipeIngredient != ingredient}" />
                        <h:selectOneMenu id="product" value="#{ingredient.product}" required="true" rendered="#{toEditRecipeIngredient == ingredient}">
                            <s:selectItems noSelectionLabel="- Select a Product -" value="#{baseProductList.resultList}" var="product" label="#{product.name}" />
                            <s:convertEntity />
                        </h:selectOneMenu>
                        <rich:toolTip for="product" rendered="#{not empty facesMessages.getCurrentMessagesForControl('product')}">
                            <h:graphicImage value="/image/error.gif" styleClass="errors" />
                            <h:message for="product" />
                        </rich:toolTip>
                    </rich:column>
                    <rich:column>
                        <f:facet name="header">Hint</f:facet>
                        <h:outputText value="#{ingredient.hint}" rendered="#{toEditRecipeIngredient != ingredient}"/>
                        <h:inputText value="#{ingredient.hint}" rendered="#{toEditRecipeIngredient == ingredient}" />
                    </rich:column>
                    <rich:column>
                        <f:facet name="header">Actions</f:facet>
                        <h:panelGroup rendered="#{toEditRecipeIngredient != ingredient}" >
                            <a4j:commandLink id="edit" value="Edit" action="#{recipeHome.editRecipeIngredient}" reRender="ingredientList" ajaxSingle="true" />
                            #{' '}
                            <a4j:commandLink id="delete" value="Delete" action="#{recipeHome.deleteRecipeIngredient}" reRender="ingredientList" ajaxSingle="true" />
                        </h:panelGroup>
                        <h:panelGroup rendered="#{toEditRecipeIngredient == ingredient}" >
                            <a4j:commandLink id="save" value="Save" action="#{recipeHome.saveEditRecipeIngredient}" reRender="ingredientList" />
                            #{' '}
                            <a4j:commandLink id="cancel" value="Cancel" action="#{recipeHome.cancelEditRecipeIngredient}" reRender="ingredientList" ajaxSingle="true" />
                        </h:panelGroup>
                    </rich:column>
                    <f:facet name="footer">
                        <h:panelGroup>
                            <a4j:commandButton id="add" value="Add" action="#{recipeHome.addRecipeIngredient}" reRender="ingredientList" ajaxSingle="true" />
                        </h:panelGroup>
                    </f:facet>
            </rich:dataTable>
            </h:form>
            <div style="clear:both"/>
        </rich:panel>

        <div class="actionButtons">
            <h:commandButton id="save"
                          value="Save"
                         action="#{recipeHome.persist}"
                       rendered="#{!recipeHome.managed}"/>
            <h:commandButton id="update"
                          value="Save"
                         action="#{recipeHome.update}"
                       rendered="#{recipeHome.managed}"/>
            <h:commandButton id="delete"
                          value="Delete"
                         action="#{recipeHome.remove}"
                      immediate="true"
                       rendered="#{recipeHome.managed}"/>
            <s:button propagation="end"
                               id="cancel"
                            value="Cancel"
                             view="/view/recipeList.xhtml"/>
        </div>
    </h:form>
</ui:define>
</ui:composition>
